package auctionsniper.ui;

import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout; 
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;



@SuppressWarnings("serial")
public class MainWindow extends JFrame implements EventListener
{

	public static final String MAIN_WINDOW_NAME = "Auction Sniper Project";
	private GridBagLayout layout;
	private GridBagConstraints constraints;
	public JTextField userfield = new JTextField(10);
	public JPasswordField passfield = new JPasswordField(10);
	public JPasswordField compassfield= new JPasswordField(10);
	public JTextField emailfield= new JTextField(10);
	private JButton buttonapply= new JButton("Sign Me Up!");
	private JLabel userlabel =  new  JLabel("Username", Label.RIGHT);
	private JLabel passlabel = new JLabel("Password", Label.RIGHT);
	private JLabel compasslabel= new  JLabel("Comfirm Password", Label.RIGHT);
	private JLabel emaillabel=  new JLabel("Email",Label.RIGHT);
	private JCheckBox terms = new JCheckBox("I agree with terms");
	boolean userSet = false;
	public JButton restartBtn = new JButton("reset");
	
public MainWindow() {
		super("New AuctionSniper User");
		setLayout(new FlowLayout());
		layout = new GridBagLayout();
		setLayout(layout);
		setLocation(400,200);
		constraints = new GridBagConstraints();
		setName(MainWindow.MAIN_WINDOW_NAME);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(400,200);
		setResizable(false);
		setVisible(true);
		Font currentFont = new Font("TimesRoman", Font.BOLD, 18);
		Font labelFont = new Font ("Comic Sans MS", Font.BOLD,14);
		
        //emailfield.addMouseListener(new MouseAdapter(){@Override public void mouseClicked(MouseEvent e){
         //       emailfield.setText("");} });
		
		userlabel.setFont(labelFont);
		passlabel.setFont(labelFont);
		compasslabel.setFont(labelFont);
		emaillabel.setFont(labelFont);
		userfield.setToolTipText("Insert your username!");
		passfield.setToolTipText("Insert your password!");
		compassfield.setToolTipText("Please comfirm your password!");
		emaillabel.setToolTipText("Insert your email");
		buttonapply.setBackground(Color.ORANGE);
		buttonapply.setFont(currentFont);
		restartBtn.setBackground(Color.LIGHT_GRAY);
		restartBtn.setFont(getFont());
		
		addComponent(userlabel,0,0,1,1);
		constraints.fill = GridBagConstraints.CENTER;
		addComponent(userfield,0,1,1,1);
		addComponent(passlabel,2,0,1,1);
		addComponent(passfield,2,1,1,1);
		addComponent(compasslabel,3,0,1,1);
		addComponent(compassfield,3,1,1,1);
		addComponent(emaillabel ,4,0,1,1);
		addComponent(emailfield,4,1,1,1);
		addComponent(terms,5,1,1,3);
		addComponent(restartBtn,7,0,1,3);
		addComponent(buttonapply,7,2,1,3);
		
		buttonapply.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
		buttonapply.setFocusable(true);
		buttonapply.addActionListener(new EnableButton());
		
		restartBtn.addActionListener(new ActionListener(){
		    public void actionPerformed(ActionEvent e){
		        userfield.setText("");
		        passfield.setText("");
		        compassfield.setText(null);
		        emailfield.setText(null);
		        //textfield.setText(null); //or use this
		    }
		});
}

private void addComponent(Component component, int i, int j, int k, int l) {
	// TODO Auto-generated method stub
	constraints.gridx = j;
	constraints.gridy = i;
	constraints.gridwidth = k;
	constraints.gridheight = l;
	layout.setConstraints(component, constraints);
	add(component);
}


public class EnableButton implements ActionListener {
	public void actionPerformed(ActionEvent e) 
	{
	
			String text = userfield.getText();
			String email = emailfield.getText();
        	JOptionPane.showMessageDialog(MainWindow.this, String.format("Hi,"+text + "\nComfirm your registration via your email:\n" + email));
		
		//catch() {
		//	JOptionPane.showMessageDialog(null, "The value must be a numeric value. ");
		//	
		//}
	//JOptionPane.showConfirmDialog(MainWindow.this, String.format
	//		("Your registration successful!!! Do you want to connect to the auction?",e.getActionCommand(),JOptionPane.PLAIN_MESSAGE));
			
	}
}
}

//public static void main(String[] args)
//{
//	javax.swing.SwingUtilities.invokeLater(new Runnable(){
//		public void run(){
//			createAndShowGUI();
//		}
//	});
//		}
//public static void createAndShowGUI() {
//		MainWindow object = new MainWindow();
//}